<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="ID" width="80"> </el-table-column>
      <el-table-column prop="catename" label="名称" width="120"> </el-table-column>
      <el-table-column label="图片" width="110">
        <template slot-scope="scope">
          <img v-if="scope.row.img" :src="'http://localhost:3000'+scope.row.img" width="100" />
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="edit(scope.row)"
            circle
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            @click="del(scope.row.id)"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getCateListTree } from "../../apis";
export default {
  data() {
    return {
      tableData: [],
    };
  },
  created() {
    this.getTableData();
  },
  methods: {
    async getTableData() {
      let res = await getCateListTree();
      this.tableData = res.list;
    },
    edit(info) {
      // 点击编辑按钮 要通过index组件打开info组件 并传值
      this.$emit("edit", info);
    },
    del(id) {
      // 打开弹窗，询问是否真的删除
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 点击确定按钮就执行then方法中的内容
          // 此时 真的删除了
          let res = await delMenuById(id);
          if (res.code == 200) {
            // 删除成功后弹窗
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            // 删除的接口中有最新的数据，所以这个情况下可以少发一次请求
            this.tableData = res.list;
          }
        })
        .catch(() => {
          // 点击确定按钮就执行catch方法中的内容
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      console.log(id);
    },
  },
};
</script>

<style>
</style>